package com.boolib.commons.aop.entity;


import com.boolib.commons.Const;
import com.boolib.commons.aop.exception.child.UserIsNotLoginException;
import com.boolib.pojo.ShopUser;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;


@Slf4j
@Aspect
@Component
public class LoginAspect {

    @Pointcut("@annotation(org.springframework.boot.autoconfigure.SpringBootApplication)")
    public void start(){}

    @Pointcut("@annotation(com.boolib.commons.aop.annotation.IsLogin)")
    public void isLogin(){}

    @Pointcut("@annotation(com.boolib.commons.aop.annotation.IsAdmin)")
    public void isAdmin(){}






    @Before("isLogin()")
    public void authLogin () throws Throwable {


        HttpSession session =( (ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest().getSession();
        ShopUser user = (ShopUser)session.getAttribute(Const.User.SESSIONAUTH);

        if(user == null){
            throw new UserIsNotLoginException("用户尚未登录");
        }


    }
//
//
//    @Before("isAdmin()")
//    public void authAdmin () throws Throwable {
//
//        HttpSession session = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest().getSession();
//
//        IUser user = (ShopUser)session.getAttribute(Const.User.SESSIONAUTH);
//
//        if(user == null){
//            throw new UserIsNotLoginException("用户尚未登录");
//        }
//
//        if(!user.isAdmin()){
//            throw  new UserNotPermissionsException("用户无权限");
//        }
//
//    }








}




